<?php
/**
* 学员管理
* @date: 2017年7月28日 下午11:03:08
* @author: hxzlh
*/
namespace app\admin\controller;

use think\Request;
use app\admin\model\Student as StudentMode;
use app\admin\model\Course;
use app\admin\model\Clbum;
use app\admin\model\StudentAttend;
use app\admin\model\StudentCourse;
use app\admin\model\StudentClbum;
use app\admin\model\PayLog;

class Student extends Admin{

    /**
     * XXX
     * 学生管理列表
     */
    public function index(){
        if (Request::instance()->has('name','param')) {
            $name = Request::instance()->param('name');
            if(is_numeric($name)){
                $map['id|name']=   array('like','%'.$name.'%');
            }else{
                $map['name']    =   array('like', '%'.(string)$name.'%');
            }
        }
        if (Request::instance()->has('state','param')) {
            $map['state']    =  Request::instance()->param('state');
        }

        $list   = $this->lists('Student', $map, 'update_time desc');
        int_to_string($list, ['state'=>['禁用','正常','已体验','已购课','课程已结束'],'gender'=>['未知','男','女']]);
        $this->assign('_list', $list);
        $this->assign('meta_title','学生列表');
        return $this->fetch();
    }

    /**
     * XXX
     * 编辑课程
     * @date: 2017年7月27日 下午11:59:13
     * @return: return
     */
    public function edit(){
        $model = new StudentMode();
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'Student');
            if(true !== $result){
                return $this->error($result);
            }
            $res = $model->update($data);
            if($res){
                $this->success('编辑成功！', url('index'));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('id');
            empty($id) && $this->error('参数不能为空！');

            $info = $model->find($id);
            $this->assign('_info',       $info);
            $this->assign('meta_title' ,'编辑学生');
            return $this->fetch();
        }
    }
    /**
     * XXX
     * 新增课程
     * @date: 2017年7月27日 下午11:07:29
     * @return mixed|string
     * @return: return
     */
    public function add(){
        if(Request::instance()->isPost()){
            //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'Student');
            if(true !== $result){
                return $this->error($result);
            }
            $res = StudentMode::create($data);
            if($data){
                $this->success('新增成功','index');
            } else {
                $this->error($result);
            }
        } else {
            $info = [];
            $this->assign('_info', $info);
            $this->assign('meta_title' ,'新增学生');
            return $this->fetch('edit');
        }
    }
    /**
     * XXX
     * 更改课程状态
     * @date: 2017年7月28日 上午12:25:21
     * @return: return
     */
    public function changeState($method=null)
    {
        $data = Request::instance()->param('id/a');
        $id = array_unique($data);
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbid':
                $this->forbid('student', $map );
                break;
            case 'resume':
                $this->resume('student', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    /********************* 选择课程开始 ***************************/
    //课程信息
    public function course() 
    {
        $id = Request::instance()->param('id');
        empty($id) && $this->error('参数不能为空！');
        $where['student_id'] = $id;
        $list = $this->lists('StudentCourse', $where, 'update_time desc');
        $this->assign('_list', $list);
        return $this->fetch();
    }
    //添加课程
    public function addCourse() 
    {
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'StudentCourse');
            if(true !== $result){
                return $this->error($result);
            }
            $res = StudentCourse::create($data);
            if($res){
                $this->success('新增成功！', url('course',['id'=>$data['student_id']]));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('student_id');
            empty($id) && $this->error('参数不能为空！');
            $info['student_id'] = $id;
            $this->assign('_info', $info);
            $this->assign('meta_title' ,'添加课程');
            return $this->fetch('editcourse');
        }
    }
    //修改课程
    public function editCourse()
    {
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'StudentCourse');
            if(true !== $result){
                return $this->error($result);
            }
            $res = StudentCourse::update($data);
            if($res){
                $this->success('编辑成功！', url('course',['id'=>$data['student_id']]));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('id');
            empty($id) && $this->error('参数不能为空！');
            $info = StudentCourse::get($id);
            
            $this->assign('_info', $info);
            $this->assign('meta_title' ,'编辑课程');
            return $this->fetch('editcourse');
        }
    }
    /********************* 选择课程结束 ***************************/
    
    /********************* 所在班级开始 ***************************/
    //班级信息
    public function clbum()
    {
        $id = Request::instance()->param('id');
        empty($id) && $this->error('参数不能为空！');
        $model = \think\Loader::model('Student','service');
        $list = $model->getStudentClbums($id);
        $page = $list->render();
        $this->assign('_page', $page);
        $this->assign('_list', $list);
        return $this->fetch();
    }
    //退出班级
    public function quitClbum() 
    {
        $student_id = Request::instance()->param('student_id');
        empty($student_id) && $this->error('参数学生不能为空！');
        $clbum_id = Request::instance()->param('clbum_id');
        empty($clbum_id) && $this->error('参数班级不能为空！');
        $model = \think\Loader::model('Student','service');
        $res = $model->quitClbum($clbum_id, $student_id);
        if($res){
            $this->success('退出成功！');
        } else {
            $this->error('退出失败');
        }
    }
    //加入班级
    public function joinClbum() 
    {
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'StudentClbum');
            if(true !== $result){
                return $this->error($result);
            }
            $res = StudentClbum::create($data);
            if($res){
                $this->success('新增成功！', url('clbum',['id'=>$data['student_id']]));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('student_id');
            empty($id) && $this->error('参数不能为空！');
            $info['student_id'] = $id;
            $this->assign('_info', $info);
            $this->assign('meta_title' ,'加入班级');
            return $this->fetch();
        }
    }
    /********************* 所在班级结束 ***************************/
    
    /********************* 上课明细开始 ***************************/
    //上课明细
    public function attend()
    {
        $id = Request::instance()->param('id');
        empty($id) && $this->error('参数不能为空！');
        $map['student_id'] = $id;
        $list   = $this->lists('StudentAttend', $map, 'update_time desc');
        $this->assign('_list', $list);
        return $this->fetch();
    }
    //上课
    public function attendClass() 
    {
        $student_id = Request::instance()->param('student_id');
        empty($student_id) && $this->error('参数学生不能为空！');
        $clbum_id = Request::instance()->param('clbum_id');
        empty($clbum_id) && $this->error('参数班级不能为空！');
        
        $course_id= Request::instance()->param('course_id');
        empty($course_id) && $this->error('参数课程不能为空！');
        
        //查询班级信息(课程,课时)
        $clbum = Clbum::get($clbum_id);
        //计算班级课时
        $hour = round((strtotime($clbum['end_time'])-strtotime($clbum['start_time']))/3600, 2);
        //减少学生选课课时
        $data['student_id'] = $student_id;
        $data['clbum_id'] = $clbum_id;
        $data['course_id'] = $course_id;
        $data['deduction_hours'] = $hour;
        
        //验证课时
        $result = $this->validate($data,'StudentAttend');
        if(true !== $result){
            return $this->error($result);
        }
        
        $res = \think\Db::transaction(function() use($hour, $data, $clbum){
            //更新课时
            $where['student_id'] = $data['student_id'];
            $where['course_id'] = $clbum['course_id'];
            StudentCourse::where($where)->setDec('remain_hours', $hour);
            //添加记录
            StudentAttend::create($data);
        });
        
        $this->success('上课成功！');
    }
    
    //添加上课明细 TODO
    public function addAttend() 
    {
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'StudentAttend');
            if(true !== $result){
                return $this->error($result);
            }
            $res = \think\Db::transaction(function() use($data){
                $clbum = Clbum::get($data['clbum_id']);
                //更新课时
                $where['student_id'] = $data['student_id'];
                $where['course_id'] = $clbum['course_id'];
                $res1 = StudentCourse::where($where)->setDec('remain_hours', $data['deduction_hours']);
                //添加记录
                $res2 = StudentAttend::create($data);
                return $res1 && $res2 ? true : false;
            });
            if($res){
                $this->success('扣减成功！',  url('attend',['id'=>$data['student_id']]));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('student_id');
            empty($id) && $this->error('参数不能为空！');
            
            $info['student_id'] = $id;
            $this->assign('_info',       $info);
            $this->assign('meta_title' ,'扣减课时');
            return $this->fetch('editAttend');
        }
    }
    
    /********************* 上课明细结束 ***************************/
    
    /********************* 充值明细开始 ***************************/
    //充值明细
    public function pay()
    {
        $id = Request::instance()->param('id');
        empty($id) && $this->error('参数不能为空！');
        $map['student_id'] = $id;
        $list   = $this->lists('PayLog', $map, 'update_time desc');
        $this->assign('_list', $list);
        return $this->fetch();
    }
    
    /**
     * 充值/扣减课时明细
     * @date: 2017年8月2日 下午11:37:34
     * @author: hxzlh
     * @return void|mixed|string
     * @return: return
     */
    public function editHour() 
    {
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'PayLog');
            if(true !== $result){
                return $this->error($result);
            }
            
            $res = \think\Db::transaction(function() use($data){
                
                //更新课时
                $where['student_id'] = $data['student_id'];
                $where['course_id'] = $data['course_id'];
                if ($data['hour_type'] === '0') {
                    $res2 = StudentCourse::where($where)->setDec('remain_hours', $data['hours']);
                } else {
                    $res2 = StudentCourse::where($where)
                    ->inc('total_hours', $data['hours'])
                    ->inc('remain_hours', $data['hours'])
                    ->update();
                }
                unset($data['hour_type']);
                //添加记录
                $res1 = PayLog::create($data);
                return $res1 && $res2 ? true : false;
            });
            
            if($res){
                $this->success('添加成功！', url('course',['id'=>$data['student_id']]));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('id');
            empty($id) && $this->error('参数不能为空！');
            $info = StudentCourse::get($id);
            
            $this->assign('_info', $info);
            $this->assign('meta_title' ,'操作课时');
            return $this->fetch('edithour');
        }
    }
    
    
    

}
